<template>
    <div>
        <!-- 右侧悬浮折叠信息窗 -->
        <div class="collapse-box">
            <button class="toggle-button" @click="toggleCollapse">
                <span :class="isCollapse?'el-icon-notebook-2':'el-icon-caret-right'" style="margin-bottom: 5px" />
                <span>{{isCollapse?'数据记录':'收起窗口'}}</span>
            </button>
            <div :class="isCollapse?'collapse-box-hide':'collapse-box-hide collapse-box-active'">
                <div v-if="isShowFont">
                    <ul class="note-ul" v-if="equipmentRecords && accountData.data">
                        <li class="note-li" v-for="(item,index) in equipmentRecords">
                            <span class="note-highlight-blue"> {{getAccountNameByID(item.userId)}} </span>于
                            <span class="note-highlight-blue"> {{item.date}} </span>
                            <span :class="getBehaviorClass(item.behavior)"> {{getBehavior(item.behavior)}} </span>了：
                            <span class="note-highlight-blue">设备{{item.targetId}} </span>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <!-- 控制Table行的悬浮窗口 -->
        <div :class="floatBoxVisible?'float-box float-box-active':'float-box'">
            <button class="float-button" @click="tableBackup">
                <span class="el-icon-top"></span>
            </button>
            <button class="float-button" @click="tablePutdown">
                <span class="el-icon-bottom"></span>
            </button>
        </div>
        <!--Dialog 添加/编辑-->
        <el-dialog :title="editState?'编辑设备':'添加设备'" :width="editState?'60%':'40%'" :visible.sync="dialogFormVisible" :top="'8vh'" @close="closeDialog">
            <!--添加-->
            <el-form ref="addForm" :rules="equipmentRule" :model="equipmentForm" v-if="!editState">
                <el-row>
                    <el-col :span="18">
                        <el-form-item prop="areaId" label="放置区域：" label-width="100px">
                            <el-select v-model="equipmentForm.areaId" clearable filterable placeholder="请选择区域..." class="form-item-select">
                                <el-option v-for="item in areaData" v-if="item.enable" :key="item.areaId+''" :label="item.areaRemarks+' / '+item.areaName" :value="item.areaId" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="11">
                        <el-form-item prop="altitude" label="放置海拔：" label-width="100px">
                            <el-input v-model="equipmentForm.altitude" auto-complete="off"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item prop="securityCode" label="安全码：" label-width="100px">
                            <el-input v-model="equipmentForm.securityCode" auto-complete="off"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="22">
                        <el-form-item label="部署点位：" label-width="100px">
                            <MapSearch :editState="editState" :area="MapSearchAreaProp" :radius="equipmentForm.radius" @getPosition="getPosition" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="22">
                        <el-form-item label="监测半径：" label-width="100px">
                            <el-slider v-model="equipmentForm.radius" :step="10" :min="10" :max="250" :marks="radiusMarks" style="width: 100%;margin-bottom: 20px" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <!--编辑-->
            <el-form ref="editForm" :rules="equipmentRule" :model="equipmentForm" v-if="editState" label-position="left" >
                <el-row type="flex" justify="space-around">
                    <el-col :span="10">
                        <el-form-item label="设备编号：" label-width="120px">
                            <span>{{equipmentForm.equipmentId}}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="部署区域：" label-width="120px">
                            <el-select v-model="equipmentForm.areaId" clearable filterable class="form-item-select">
                                <el-option v-for="item in areaData" v-if="item.enable" :key="item.areaId" :label="item.areaRemarks+' / '+item.areaName" :value="item.areaId" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex" justify="space-around">
                    <el-col :span="22">
                        <el-form-item label="部署点位：" label-width="100px">
                            <MapSearch :selectIndex="selectIndex" :editState="editState" :area="MapSearchAreaProp" :paramPosition="selectPosition" :radius="equipmentForm.radius" @getPosition="getPosition" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex" justify="space-around">
                    <el-col :span="22">
                        <el-form-item label="监测半径：" label-width="100px">
                            <el-slider v-model="equipmentForm.radius" :step="10" :min="10" :max="250" :marks="radiusMarks" style="width: 100%;margin-bottom: 20px" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex" justify="space-around">
                    <el-col :span="10">
                        <el-form-item label="最后上传日期：" label-width="120px">
                            <el-date-picker v-model="equipmentForm.lastDate" type="datetime" placeholder="选择日期时间" :disabled="true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item prop="altitude" label="安置海拔：" label-width="120px">
                            <el-input v-model="equipmentForm.altitude" auto-complete="off" :style="{width: 220 + 'px'}" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex" justify="space-around">
                    <el-col :span="10">
                        <el-form-item prop="securityCode" label="安全码：" label-width="120px">
                            <el-input v-model="equipmentForm.securityCode" auto-complete="off" :style="{width: 220 + 'px'}" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="运行时长：" label-width="120px">
                            <span>{{equipmentForm.runtime}} h</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex" justify="space-around">
                    <el-col :span="10">
                        <el-form-item label="设备状态：" label-width="120px">
                            <span style="color: #68c176" v-if="equipmentForm.state===1"> 正 常</span>
                            <span style="color: #ff8029" v-if="equipmentForm.state===2"> 延 迟</span>
                            <span style="color: #e05e5e" v-if="equipmentForm.state===3"> 断 开</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="启用状态：" label-width="120px">
                            <el-tooltip :content="equipmentForm.enable?'点击禁用':'点击激活'" placement="bottom">
                                <el-switch v-model="equipmentForm.enable" active-color="#13ce66" inactive-color="#ff4949" :style="{height: 40 + 'px'}" />
                            </el-tooltip>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <!--添加用的按钮-->
            <el-row type="flex" justify="end" v-if="!editState">
                <el-col :span="3" :pull="1">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                </el-col>
                <el-col :span="3" :pull="0">
                    <el-button type="primary" @click="equipmentAdd">确 定</el-button>
                </el-col>
            </el-row>
            <!--编辑用的按钮-->
            <el-row type="flex" justify="end" v-if="editState">
                <el-col :span="3" :pull="1">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                </el-col>
                <el-col :span="3" :pull="0">
                    <el-button type="primary" @click="equipmentEdit">确 定</el-button>
                </el-col>
            </el-row>
        </el-dialog>
        <!--高级搜索抽屉-->
        <el-drawer title="高级搜索" size="30%" :before-close="handleClose" :visible.sync="drawerState" direction="rtl" ref="drawer">
            <el-container>
                <el-main>
                    <el-form :model="equipmentParam" status-icon id="seniorSearchDrawer" ref="selectForm" :rules="selectFormRule">
                        <el-row type="flex" justify="center">
                            <el-col :span="20">
                                <el-form-item label="设备编号" prop="equipmentId" label-width="100px">
                                    <el-input v-model="equipmentParam.equipmentId" auto-complete="off"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row type="flex" justify="center">
                            <el-col :span="20">
                                <el-form-item label="部署区域" label-width="100px">
                                    <el-select v-model="equipmentParam.areaId" placeholder="请选择部署区域">
                                        <el-option v-for="item in areaData" :key="item.areaId" :label="item.areaRemarks+' / '+item.areaName" :value="item.areaId" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row type="flex" justify="center">
                            <el-col :span="20">
                                <el-form-item label="监测半径" label-width="100px">
                                    <el-slider v-model="equipmentParam.radius" :step="10" :min="0" :max="250" :marks="radiusMarks" style="width: 100%;margin-bottom: 20px" />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row type="flex" justify="center">
                            <el-col :span="20">
                                <el-form-item label="网络状态" label-width="100px">
                                    <el-select v-model="equipmentParam.state" placeholder="请选择网路状态">
                                        <el-option label="正常" value="1"></el-option>
                                        <el-option label="延迟" value="2"></el-option>
                                        <el-option label="断开" value="3"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row type="flex" justify="center">
                            <el-col :span="20">
                                <el-form-item label="首次启用日期" label-width="100px">
                                    <el-date-picker v-model="equipmentParam.runDateScope" value-format="yyyy-MM-dd HH:mm:ss"
                                                    type="datetimerange" :picker-options="pickerOptions"
                                                    range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                                                    align="right">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row type="flex" justify="center">
                            <el-col :span="20">
                                <el-form-item label="最后上传日期" label-width="100px">
                                    <el-date-picker v-model="equipmentParam.updateDataScope" value-format="yyyy-MM-dd HH:mm:ss"
                                                    type="datetimerange" :picker-options="pickerOptions"
                                                    range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                                                    align="right">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </el-main>
                <!--提交按钮-->
                <el-footer>
                    <el-row type="flex" justify="end">
                        <el-col :span="5">
                            <el-button @click="cancelForm">取 消</el-button>
                        </el-col>
                        <el-col :span="5">
                            <el-button type="primary" @click="$refs.drawer.closeDrawer()" :loading="drawerLoading">{{ drawerLoading ? '搜索中 ...' : '提交搜索' }}</el-button>
                        </el-col>
                    </el-row>
                </el-footer>
            </el-container>
        </el-drawer>
        <!--添加/搜索响应按钮行-->
        <!-- 管理者显示 -->
        <el-row :gutter="20" v-if="loginInfo.levelId === 1">
            <el-col :xl="2" :lg="2" :md="2" :sm="2" :xs="10">
                <el-button type="primary" size="small" icon="el-icon-plus" @click="openDialogForAdd">添加</el-button>
            </el-col>
            <el-col :xl="14" :lg="14" :md="10" :sm="13" class="hidden-xs-only">
                <span class="span-font">当前显示：{{countDisplay}}</span>
            </el-col>
            <el-col :xl="4" :lg="4" :md="6" :sm="6" :xs="8">
                <el-input v-model="equipmentParam.equipmentId" size="small" placeholder="按设备编号搜索..."/>
            </el-col>
            <el-col :xl="2" :lg="2" :md="3" :sm="3" :xs="6">
                <el-button type="primary" size="small" icon="el-icon-search" @click="normalSelect">搜索</el-button>
            </el-col>
            <el-col :xl="2" :lg="2" :md="3" class="hidden-sm-and-down">
                <el-button type="primary" size="small" icon="el-icon-link" @click="openDrawer">高级搜索</el-button>
            </el-col>

<!--            <el-col :span="2">
                <el-button type="primary" size="small" icon="el-icon-plus" @click="openDialogForAdd">添加</el-button>
            </el-col>
            <el-col :span="14">
                <span class="span-font">当前显示：{{countDisplay}}</span>
            </el-col>
            <el-col :span="4">
                <el-input v-model="equipmentParam.equipmentId" size="small" placeholder="按设备编号搜索..."/>
            </el-col>
            <el-col :span="2">
                <el-button type="primary" size="small" icon="el-icon-search" @click="normalSelect">搜索</el-button>
            </el-col>
            <el-col :span="2">
                <el-button type="primary" size="small" icon="el-icon-link" @click="openDrawer">高级搜索</el-button>
            </el-col>-->
        </el-row>
        <!-- 用户显示 -->
        <el-row :gutter="20" v-if="loginInfo.levelId !== 1">
            <el-col :span="4">
                <el-input v-model="equipmentParam.equipmentId" size="small" placeholder="按设备编号搜索..."/>
            </el-col>
            <el-col :span="2">
                <el-button type="primary" size="small" icon="el-icon-search" @click="normalSelect">搜索</el-button>
            </el-col>
            <el-col :span="16">
                <span class="span-font">当前显示：{{countDisplay}}</span>
            </el-col>
            <el-col :span="2">
                <el-button type="primary" size="small" icon="el-icon-link" @click="openDrawer">高级搜索</el-button>
            </el-col>
        </el-row>
        <!-- 管理员table -->
        <el-table :data="this.equipmentData.data" stripe style="width: 100%" :height="tableHeight" v-if="loginInfo.levelId === 1"
                  ref="equipmentTable"
                  v-el-table-infinite-scroll="equipmentLoad"
                  infinite-scroll-distance="10px"
                  infinite-scroll-delay="100"
                  :border="true"
                  header-cell-class-name="header-cell"
                  :row-class-name="tableRowClassName"
                  v-loading="tableLoading">
            <!--扩展信息栏-->
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-form label-position="left" inline class="table-expand">
                        <el-form-item label="设备编号：">
                            <span>{{props.row.equipmentId}}</span>
                        </el-form-item>
                        <el-form-item label="部署区域：">
                            <span>{{props.row.area.areaRemarks}} / {{props.row.area.areaName}}</span>
                        </el-form-item>
                        <el-form-item label="经度坐标：">
                            <span>{{props.row.positionLng}}</span>
                        </el-form-item>
                        <el-form-item label="纬度坐标：">
                            <span>{{props.row.positionLat}}</span>
                        </el-form-item>
                        <el-form-item label="首次启用日期：">
                            <span>{{props.row.firstDate}}</span>
                        </el-form-item>
                        <el-form-item label="最后上传日期：">
                            <span>{{props.row.lastDate}}</span>
                        </el-form-item>
                        <el-form-item label="运行时长：">
                            <span>{{props.row.runtime}} h</span>
                        </el-form-item>
                        <el-form-item label="监测半径：">
                            <span>{{props.row.radius}} 米</span>
                        </el-form-item>
                        <el-form-item label="安置海拔：">
                            <span>{{props.row.altitude}}</span>
                        </el-form-item>
                        <el-form-item label="剩余电量：">
                            <span>{{props.row.power}} %</span>
                        </el-form-item>
                        <el-form-item label="设备状态：">
                            <span v-if="props.row.state===1" style="color: #68c176">正常</span>
                            <span v-if="props.row.state===2" style="color: #ff8029">延迟</span>
                            <span v-if="props.row.state===3" style="color: #e05e5e">断开</span>
                        </el-form-item>
                        <el-form-item label="启用状态：">
                            <span v-if="props.row.enable" style="color: #68c176">激活</span>
                            <span v-if="!props.row.enable" style="color: #e05e5e">禁用</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <!--略缩列表-->
            <el-table-column prop="equipmentId" label="ID" width="80" sortable></el-table-column>
            <el-table-column prop="area.areaName" label="部署区域" width="230" sortable></el-table-column>
            <el-table-column prop="runtime" label="运行时长" width="110" sortable align="center" header-align="left">
                <template slot-scope="scope">{{scope.row.runtime}} h</template>
            </el-table-column>
            <el-table-column prop="lastDate" label="最后上传日期" width="160" sortable></el-table-column>
            <el-table-column prop="altitude" label="放置海拔" width="110" sortable align="center" header-align="left">
                <template slot-scope="scope">{{scope.row.altitude}} 米</template>
            </el-table-column>
            <el-table-column prop="state" label="网络状态" width="110" sortable>
                <template slot-scope="scope">
                    <el-tooltip content="正常" placement="right" effect="light" v-if="scope.row.state===1">
                        <span class="table-cell-span-normal"/>
                    </el-tooltip>
                    <el-tooltip content="延迟" placement="right" effect="light" v-if="scope.row.state===2">
                        <span class="table-cell-span-lag"/>
                    </el-tooltip>
                    <el-tooltip content="断开" placement="right" effect="light" v-if="scope.row.state===3">
                        <span class="table-cell-span-error"/>
                    </el-tooltip>
                </template>
            </el-table-column>
            <el-table-column prop="power" label="剩余电量" width="120" sortable>
                <template slot-scope="scope">
                    <el-progress :text-inside="true" :percentage="scope.row.power" :stroke-width="18" :status="progresStatus(scope.row.power)"></el-progress>
                </template>
            </el-table-column>
            <el-table-column prop="enable" label="启用状态" width="110" sortable align="center" header-align="left">
                <template slot-scope="scope">
                    <span v-if="scope.row.enable" style="color: #68c176">激活</span>
                    <span v-if="!scope.row.enable" style="color: #e05e5e">禁用</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="200" v-if="loginInfo.levelId === 1" align="center" header-align="left">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary" icon="el-icon-edit" class="table-cell-button"
                               @click="openDialogForEdit(scope.$index,scope.row)">编辑</el-button>
                    <el-popconfirm title="确定要重置该账户吗?" confirm-button-text="确定" cancel-button-text="取消"
                                   @confirm="reset(scope.$index,scope.row)" icon-color="red" icon="el-icon-info">
                        <el-button size="mini" type="danger" icon="el-icon-warning" slot="reference" class="table-cell-button">重 置</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 用户table -->
        <el-table :data="this.equipmentData.data" stripe style="width: 100%" :height="tableHeight" v-if="loginInfo.levelId !== 1"
                  ref="equipmentTable"
                  v-el-table-infinite-scroll="equipmentLoad"
                  infinite-scroll-distance="10px"
                  infinite-scroll-delay="100"
                  :border="true"
                  header-cell-class-name="header-cell"
                  v-loading="tableLoading">
            <!--扩展信息栏-->
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-form label-position="left" inline class="table-expand">
                        <el-form-item label="设备编号：">
                            <span>{{props.row.equipmentId}}</span>
                        </el-form-item>
                        <el-form-item label="部署区域：">
                            <span>{{props.row.area.areaRemarks}} / {{props.row.area.areaName}}</span>
                        </el-form-item>
                        <el-form-item label="最后上传日期：">
                            <span>{{props.row.lastDate}}</span>
                        </el-form-item>
                        <el-form-item label="运行时长：">
                            <span>{{props.row.runtime}} h</span>
                        </el-form-item>
                        <el-form-item label="监测半径：">
                            <span>{{props.row.radius}} 米</span>
                        </el-form-item>
                        <el-form-item label="安置海拔：">
                            <span>{{props.row.altitude}}</span>
                        </el-form-item>
                        <el-form-item label="剩余电量：">
                            <span>{{props.row.power}} %</span>
                        </el-form-item>
                        <el-form-item label="设备状态：">
                            <span v-if="props.row.state===1" style="color: #68c176">正常</span>
                            <span v-if="props.row.state===2" style="color: #ff8029">延迟</span>
                            <span v-if="props.row.state===3" style="color: #e05e5e">断开</span>
                        </el-form-item>
                        <el-form-item label="启用状态：">
                            <span v-if="props.row.enable" style="color: #68c176">激活</span>
                            <span v-if="!props.row.enable" style="color: #e05e5e">禁用</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <!--略缩列表-->
            <el-table-column prop="equipmentId" label="ID" width="80" sortable></el-table-column>
            <el-table-column prop="area.areaName" label="部署区域" width="230" sortable></el-table-column>
            <el-table-column prop="runtime" label="运行时长" width="120" sortable>
                <template slot-scope="scope">{{scope.row.runtime}} h</template>
            </el-table-column>
            <el-table-column prop="lastDate" label="最后上传日期" width="180" sortable></el-table-column>
            <el-table-column prop="state" label="网络状态" align="center" header-align="left" width="110" sortable>
                <template slot-scope="scope">
                    <el-tooltip content="正常" placement="right" effect="light" v-if="scope.row.state===1">
                        <span class="table-cell-span-normal"/>
                    </el-tooltip>
                    <el-tooltip content="延迟" placement="right" effect="light" v-if="scope.row.state===2">
                        <span class="table-cell-span-lag"/>
                    </el-tooltip>
                    <el-tooltip content="断开" placement="right" effect="light" v-if="scope.row.state===3">
                        <span class="table-cell-span-error"/>
                    </el-tooltip>
                </template>
            </el-table-column>
            <el-table-column prop="power" label="剩余电量" align="center" header-align="left" width="140" sortable>
                <template slot-scope="scope">
                    <el-progress :text-inside="true" :percentage="scope.row.power" :stroke-width="18" :status="progresStatus(scope.row.power)"></el-progress>
                </template>
            </el-table-column>
            <el-table-column prop="enable" label="启用状态" width="130" align="center" header-align="left">
                <template slot-scope="scope">
                    <span v-if="scope.row.enable" style="color: #68c176">激活</span>
                    <span v-if="!scope.row.enable" style="color: #e05e5e">禁用</span>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import elTableInfiniteScroll from "el-table-infinite-scroll";
import MapSearch from "../common/MapSearch"
import {mapState,mapActions} from 'vuex'

export default {
    name: "EquipmentList",
    directives: {
        'el-table-infinite-scroll': elTableInfiniteScroll
    },
    components: {
        MapSearch
    },
    data() {
        let validateId = (rule,value,callback)=>{
            if(value !== ''){
                let reg = /^[0-9]\d*$/;
                if(reg.test(value)){
                    callback();
                }else{
                    callback(new Error("请输入数据的正整数编号"));
                }
            }
            callback();
        }
        return {
            equipmentParam: {
                currentPage: 1,
                pageSize: 20,
                equipmentId: '',
                areaId: '',
                state: '',
                radius: 0,
                runDateScope: [],
                updateDataScope: []
            },
            //提交高级检索条件的表单验证规则
            selectFormRule: {
                equipmentId: [
                    {validator: validateId, trigger: 'blur'}
                ]
            },
            //添加新设备时表单的验证规则
            equipmentRule: {
                areaId: [
                    {required: true, message: '请选择部署区域', trigger: 'blur'}
                ],
                altitude: [
                    {required: true, message: '请输入安置海拔', trigger: 'blur'}
                ],
                securityCode: [
                    {required: true, message: '请输入设备安全码', trigger: 'blur'}
                ],
                radius: [
                    {require: true, message: '请选择作用范围', trigger: 'blur'}
                ]
            },
            dialogFormVisible: false,
            floatBoxVisible: false,
            isCollapse: true,
            isShowFont: false,
            equipmentForm: {
                equipmentId: '',
                positionLng: '',
                positionLat: '',
                areaId: '',
                firstDate: '',
                lastDate: '',
                runtime: 0,
                radius: 100,
                altitude: '',
                power: 0,
                state: 1,
                securityCode: '',
                enable: true
            },
            editState: true,
            radiusMarks: {
                20: '室内精度',
                100: '默认值',
                200: '远距模式'
            },
            selectIndex: -1,
            selectPosition: [],
            drawerState: false,
            drawerLoading: false,
            timer: null,
            tableLoading: false,
            tableHeight: null,
            //供日期选择器快捷选项使用
            pickerOptions: {
                shortcuts: [
                    {
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    },
                    {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    },
                    {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }
                ]
            }
        }
    },
    methods: {
        ...mapActions([
            'updateEquipmentsParam',
            'initEquipments',
            'getAllEquipments',
            'initAreas',
            'initAccounts',
            'selectEquipments',
            'addEquipment',
            'addEquipments',
            'editEquipment'
        ]),
        equipmentAdd() {
            this.$refs.addForm.validate((valid) => {
                if (valid) {
                    //深拷贝后加密安全码,再作为参数提交给请求函数
                    let addForm = JSON.parse(JSON.stringify(this.equipmentForm));
                    addForm.securityCode = this.$md5(addForm.securityCode);
                    //调用actions中的添加设备方法
                    this.addEquipment(addForm);
                    //关闭Dialog
                    this.closeDialog();
                }
            })
        },
        equipmentEdit() {
            this.$refs.editForm.validate((valid) => {
                if (valid) {
                    //深拷贝
                    let data = JSON.parse(JSON.stringify(this.equipmentForm));
                    data.area = JSON.parse(JSON.stringify(this.getAreaById(data.areaId)));
                    //加密安全码
                    data.securityCode = this.$md5(data.securityCode);
                    //拼装参数对象
                    let params = {
                        data: data
                    }
                    //调用actions中的编辑设备方法
                    this.editEquipment(params).then(resp => {
                        //关闭Dialog
                        this.closeDialog();
                    })
                }
            })
        },
        progresStatus(power) {
            if (power > 80) {
                return "success";
            }
            if (power > 40) {
                return "warning";
            }
            return "exception";
        },
        openDialogForAdd() {
            this.dialogFormVisible = true;
            this.editState = false;
            this.getAllEquipments();
        },
        getPosition(data) {
            this.equipmentForm.positionLng = data[0];
            this.equipmentForm.positionLat = data[1];
        },
        openDialogForEdit(index, data) {
            this.dialogFormVisible = true;
            this.selectIndex = index;
            this.editState = true;
            this.equipmentForm = JSON.parse(JSON.stringify(data));
            this.equipmentForm.securityCode = '';
            //填入选中设备的坐标值,转变为传入子组件的数组参数
            this.selectPosition = [this.equipmentForm.positionLng, this.equipmentForm.positionLat];
        },
        closeDialog() {
            this.dialogFormVisible = false;
            //初始化equipmentForm
            this.equipmentForm.equipmentId = '';
            this.equipmentForm.positionLng = '';
            this.equipmentForm.positionLat = '';
            this.equipmentForm.areaId = '';
            this.equipmentForm.firstDate = '';
            this.equipmentForm.lastDate = '';
            this.equipmentForm.runtime = 0;
            this.equipmentForm.radius = 100;
            this.equipmentForm.altitude = '';
            this.equipmentForm.power = 100;
            this.equipmentForm.state = 3;
            this.equipmentForm.securityCode = '';
            this.equipmentForm.enable = true;
            //清空子组件数组参数的内容
            this.selectPosition = [];
        },
        getTableHeight() {
            this.tableHeight = window.innerHeight - 60 - (40 + 14 + 30) - 40 - (33 + 20);
        },
        tableRowClassName({row,rowIndex}) {
            if ((rowIndex + 1) % 3 === 0) {
                if ((rowIndex + 1) / 3 % 2 === 0) {
                    return 'row-background-default row-background-blue';
                } else {
                    return 'row-background-default row-background-grey';
                }
            } else {
                return 'row-background-default';
            }
        },
        tableBackup(){
            // this.$refs.equipmentTable.bodyWrapper.scrollTop = 0;
            let dom = this.$refs.equipmentTable;
            let step = Math.ceil(dom.bodyWrapper.scrollTop / 15);
            let timer = setInterval(function (){
                let nowPosition = dom.bodyWrapper.scrollTop;
                if(nowPosition > step){
                    dom.bodyWrapper.scrollTop = nowPosition - step;
                }else{
                    dom.bodyWrapper.scrollTop = 0;
                    clearInterval(timer);
                }
            },30)
        },
        tablePutdown(){
            // this.$refs.equipmentTable.bodyWrapper.scrollTop = this.$refs.equipmentTable.bodyWrapper.scrollHeight;
            let dom = this.$refs.equipmentTable;
            let nowPosition = dom.bodyWrapper.scrollTop;
            let totalHeight = dom.bodyWrapper.scrollHeight;
            let clientHeight = dom.bodyWrapper.clientHeight;
            let step = Math.ceil((totalHeight - nowPosition) / 15);
            let timer = setInterval(function (){
                let left = totalHeight - dom.bodyWrapper.scrollTop - clientHeight;
                if(left > step){
                    dom.bodyWrapper.scrollTop += step;
                }else{
                    dom.bodyWrapper.scrollTop = totalHeight;
                    clearInterval(timer);
                }
            },30)
        },
        equipmentLoad() {
            if (this.equipmentListParams.pageSize * this.equipmentListParams.currentPage < this.equipmentData.total) {
                //翻页(通过commit更新整个equipmentListParams)
                let params = this.equipmentListParams;
                params.currentPage++;
                this.updateEquipmentsParam(params);
                //开启加载动画
                this.tableLoading = true;
                //发送异步请求
                this.addEquipments();
                //设置加载动画结束计时器
                this.timer = setTimeout(() => {
                    this.tableLoading = false;
                    this.$message.success("加载第" + this.equipmentListParams.currentPage + "页");
                }, 1000);
            } else {
                this.$message.info("已到底");
            }
        },
        openDrawer() {
            this.drawerState = true;
            this.initEquipmentParam();
        },
        handleClose(done) {
            if (this.drawerLoading) {
                return;
            }
            this.$confirm('确定要提交表单吗？').then(_ => {
                this.$refs.selectForm.validate(valid=>{
                    if(valid){
                        this.tableLoading = true;
                        this.drawerLoading = true;
                        this.timer = setTimeout(() => {
                            done();
                            // 动画关闭需要一定的时间
                            setTimeout(() => {
                                this.tableLoading = false;
                                this.drawerLoading = false;
                            }, 400);
                        }, 1500);
                        //提交参数状态到Vuex中
                        this.updateEquipmentsParam(this.equipmentParam)
                        //提交搜索请求
                        this.selectEquipments();
                    }else{
                        this.$message.error("表单验证不通过")
                    }
                })
            }).catch(_ => {
                this.cancelForm();
            })
        },
        cancelForm() {
            this.drawerLoading = false;
            this.drawerState = false;
            clearTimeout(this.timer);
            this.initEquipmentParam();
        },
        initEquipmentParam() {
            //初始化Param
            this.equipmentParam.currentPage = 1;
            this.equipmentParam.pageSize = 20;
            this.equipmentParam.equipmentId = '';
            this.equipmentParam.areaId = '';
            this.equipmentParam.state = '';
            this.equipmentParam.radius = 0;
            this.equipmentParam.runDateScope = [];
            this.equipmentParam.updateDataScope = [];
        },
        normalSelect() {
            //打开载入动画
            this.tableLoading = true;
            //返回到表格顶部
            this.tableBackup();
            if (!this.equipmentParam.equipmentId) {
                //初始化userParam
                this.initEquipmentParam();
            }
            //提交状态到Vuex
            this.updateEquipmentsParam(this.equipmentParam);
            //提交搜索请求
            this.selectEquipments();
            //延时动画设置
            setTimeout(() => {
                this.tableLoading = false;
            }, 400)
        },
        getAreaById(id) {
            return this.areaData.find(item => item.areaId === id);
        },
        toggleCollapse(){
            if(this.isCollapse){
                this.isCollapse = false;
                setTimeout(_ => {
                    this.isShowFont = true;
                },500);
            }else{
                this.isCollapse = true;
                this.isShowFont = false;
            }
        },
        getAccountNameByID(userID){
            return this.accountData.data.find(item => item.userId === userID).userName;
        },
        getBehavior(behavior){
            switch (behavior){
                case 1: return '添加';
                case 2: return '编辑';
                case 3: return '重置';
                default: return '';
            }
        },
        getBehaviorClass(behavior){
            switch (behavior){
                case 1: return 'note-highlight-green';
                case 2: return 'note-highlight-yellow';
                case 3: return 'note-highlight-red';
                default: return '';
            }
        }
    },
    computed: {
        ...mapState({
            equipmentData: state => state.active.equipmentData,
            equipmentListParams: state => state.static.equipmentListParams,
            areaData: state => state.active.areaData,
            accountData: state => state.active.accountData,
            equipmentRecords: state => state.active.recordData.equipment,
            loginInfo: state => state.static.loginInfo
        }),
        MapSearchAreaProp() {
            for (let item of this.areaData) {
                if (item.areaId === this.equipmentForm.areaId) {
                    return item;
                }
            }
        },
        countDisplay(){
            if(this.equipmentData.data){
                return this.equipmentData.data.length + '/' + this.equipmentData.total;
            }
            return "";
        }
    },
    created() {
        //绑定事件来适应窗口大小的变化,做到自适应高度
        window.addEventListener("resize", this.getTableHeight, false);
        this.getTableHeight();

        //将页码重新设置成第一页
        let params = this.equipmentListParams;
        params.currentPage = 1;
        this.updateEquipmentsParam(params);

        //初始化区域数据(给<el-select>用)
        this.initAreas();
        //初始化设备数据,获取第一页数据
        this.initEquipments();
        //初始化Account数据
        this.initAccounts();
    },
    mounted() {
        //为el-table绑定滚动条事件
        //获取table的dom对象
        this.dom = this.$refs.equipmentTable.bodyWrapper;
        this.dom.addEventListener('scroll',()=>{
            //获取滚动的距离
            let scrollTop = this.dom.scrollTop;
            if(scrollTop >= 400){
                if(!this.floatBoxVisible){
                    this.floatBoxVisible = true;
                }
            }else{
                if(this.floatBoxVisible){
                    this.floatBoxVisible = false;
                }
            }
        })
    }
}
</script>

<style scoped>
.el-row{
    margin-bottom: 20px;
}

.el-row:last-child{
    margin-bottom: 0px;
}

::v-deep .row-background-default{
    background-color: #ffffff;
}
::v-deep .row-background-default:hover{
    transition: background-color .15s;
    background-color: #f5f7fa;
}

::v-deep .row-background-grey{
    background-color: #f9f9f9;
}

::v-deep .row-background-blue{
    background-color: #f5faff;
}

::v-deep .header-cell{
    font-size: 14px;
    font-weight: bold;
    color: #606266;
    background-color: #ffffff00 !important;
}

::v-deep .has-gutter tr{
    background-image: linear-gradient(120deg, #fdfbfb 0%, #ebedee 100%);
}

::v-deep .el-table__cell{
    background-color: #ffffff00 !important;
}

::v-deep .el-table__body-wrapper::-webkit-scrollbar{
    width: 8px;
}

::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb{
    background-color: #eaeaea;
    border-radius: 8px;
}

.form-item-select{
    width: 100%;
}

.table-cell-button{
    padding: 7px;
    margin-right: 10px;
}

.table-cell-button-alert{
    background-color: #ff8029;
    border-color: #ff8029;
}

.table-cell-button-alert:hover{
    background-color: #ff8029cc;
    border-color: #ff8029cc;
}

.table-cell-span-normal{
    display: block;
    width: 10px;
    height: 10px;
    margin: auto;
    border-radius: 10px;
    background-color: #68c176;
}

.table-cell-span-lag{
    display: block;
    width: 10px;
    height: 10px;
    margin: auto;
    border-radius: 10px;
    background-color: #ff8029;
}

.table-cell-span-error{
    display: block;
    width: 10px;
    height: 10px;
    margin: auto;
    border-radius: 10px;
    background-color: #e05e5e;
}

.span-font{
    float: right;
    font-size: 13px;
    font-weight: bold;
    color: #606266;
    height: 100%;
    line-height: 33px;
}

.el-manage-button{
    margin-left: 10px;
    float: right;
}

::v-deep .el-progress{
    width: 80%;
}

::v-deep #seniorSearchDrawer .el-form-item__label{
    text-align: left;
}

.table-expand{
    font-size: 0;
}

::v-deep .table-expand label{
    width: 110px;
    color: #99a9bf;
}

::v-deep .table-expand .el-form-item{
    padding-left: 20px;
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
}

/*去除在使用el-input时,type='number'后出现的两个上下箭头*/
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

::v-deep input[type="number"]{
    -moz-appearance: textfield;
}

::v-deep .el-progress{
    width: 100%;
}

.float-box{
    position: fixed;
    right: 50px;
    bottom: 100px;

    display: flex;
    flex-direction: column;
    align-items: center;
    justify-items: center;
    justify-content: space-around;

    width: 50px;
    height: 120px;
    font-size: 20px;
    border: 0;
    z-index: 5;

    transition: all 0.6s ease-in-out;
    opacity: 0;
}

.float-box-active{
    opacity: 1;
}

.float-button{
    height: 50px;
    width: 50px;
    background-color: #66b1ff;
    box-shadow: rgba(0, 0, 0, 0.12) 0 0 6px;
    text-align: center;
    line-height: 50px;
    color: #ffffff;
    border: 0;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s ease-in-out;
}

.float-button:hover{
    background-color: #409eff;
}

.collapse-box{
    position: fixed;
    right: 28px;
    top: 273px;
    border: 0;
    z-index: 5;
    transition: all 0.6s;
}

.toggle-button{
    width: 30px;
    height: 150px;
    writing-mode: horizontal-tb;
    border: 0;
    background-color: #66b1ff;
    color: #ffffff;
    border-bottom-left-radius: 10px;
    border-top-left-radius: 10px;
    font-size: 15px;
    float: left;
    cursor: pointer;
    transition: all 0.4s;
}

.toggle-button:hover{
    background-color: #409eff;
}

.collapse-box-hide{
    width: 0px;
    height: 150px;
    float: left;
    background-image: linear-gradient(to right bottom, rgba(236, 245, 255, 1), rgba(235, 237, 238, 0.7));
    cursor: default;
    transition: all 0.6s;
}

.collapse-box-active{
    width: 350px;
    height: 300px;
    border-bottom-left-radius: 10px;
}

.note-li{
    font-size: 13px;
    margin-top: 10px;
}

.note-highlight-blue{
    color: #40a9ff;
}

.note-highlight-green{
    color: #67c23a;
}

.note-highlight-yellow{
    color: #e6a23c;
}

.note-highlight-red{
    color: #e05e5e;
}

::v-deep .has-gutter .gutter{
    background-color: #ffffff !important;
    border: 0 !important;
}
</style>